const view={
    getValueByPosArr(posArr,rect){
        return rect.data[posArr[0]+posArr[1]*rect.width]
    },
    //通过花费得到的收益
    initItem(item,all){
        const rect1=this.rect1
        const rect2=this.rect2
        const posArr1=this.posArr1
        const posArr2=this.posArr2

        while(item.price>0){
            const pos1=[posArr1[item.id][0]+posArr2[item.curFee][0],posArr1[item.id][1]+posArr2[item.curFee][1]]
            const pos2=posArr2[item.curFee]
            item.curFee++
            item.price--
            if(this.getValueByPosArr(pos1,rect1)===this.getValueByPosArr(pos2,rect2)){
                item.val++
            }else if(!all){
                break
            }else if(all!==true){
                all--
            }
        }
    },
    //大图中查找小图,获取最大的相似区域
    getMaxList(rect1,rect2,pipei){
        this.rect1=rect1
        this.rect2=rect2
        this.posArr1=[]
        this.posArr2=[]
        this.taskList=[]
        const posArr1=this.posArr1
        const posArr2=this.posArr2


// 初始化任务列表
        let taskList=this.taskList
        let id = 0;

        const len2=rect2.width*rect2.height
// 遍历 bigRect 的每个像素点，生成任务
        const h1=rect1.height-rect2.height+1
        const w1=rect1.width-rect2.width+1
        for (let y = 0; y < h1; y++) {
            for (let x = 0; x < w1; x++) {
                posArr1.push([x,y])
                taskList.push({
                    id: id,          // 唯一任务 ID
                    price: len2,     // 价格
                    curFee:0,   //花费
                    val:0,    //价值
                });
                id++
            }
        }
        for (let y = 0; y < rect2.height; y++) {
            for (let x = 0; x < rect2.width; x++) {
                posArr2.push([x,y])
            }
        }

        //一直找到val最大的，
        let maxVal=0

        let isRunning=true

        while (isRunning){
            let maxList=[]
            taskList.forEach( (item)=>{
                this.initItem(item)
                if (item.val > maxVal) {
                    maxVal = item.val
                    maxList=[item]
                }else if (item.val === maxVal) {
                    maxList.push(item)
                }
            })
            if(taskList.length===maxList.length){
                maxList=[]
                taskList.forEach((item)=>{
                    this.initItem(item,10)
                    if (item.val > maxVal) {
                        maxVal = item.val
                        maxList=[item]
                    }else if (item.val === maxVal) {
                        maxList.push(item)
                    }
                })
            }
            if(taskList.length===maxList.length||maxList.length==0){
                isRunning=false
            }else{
                taskList=maxList
            }

        }

        if(taskList.length){
            taskList.forEach((item)=>{
                item.x=posArr1[item.id][0]
                item.y=posArr1[item.id][1]
                item.width=rect2.width
                item.height=rect2.height
                item.pipei=parseInt((item.val*100/item.curFee))
            })
        }
        console.log(taskList.length)
        return taskList


    },

}
module.exports=view